package com.kzx.blog.config;

import com.kzx.blog.config.model.FailedException;
import com.kzx.blog.config.model.FailedInfo;
import com.kzx.blog.config.model.ResultData;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author kongz
 * @version 1.0 2021/11/23 10:52
 * @description 全局异常检测
 */
@RestControllerAdvice(basePackages = "com.kzx.blog.controller")
public class GlobalExceptionHandler {

    @ExceptionHandler(FailedException.class)
    @ResponseBody
    public ResultData exceptionHandler(FailedException e) {
        return ResultData.error(e);
    }

    @ExceptionHandler(MethodArgumentNotValidException.class)
    @ResponseBody
    public ResultData validExceptionHandler(MethodArgumentNotValidException e) {
        return new ResultData(FailedInfo.ILLEGAL_PARAM.getErrorCode(),e.getBindingResult().getFieldError().getDefaultMessage());
    }

}
